package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.entity.AddressBook;
import com.sky.mapper.AddBookMapper;
import com.sky.service.AddBookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class AddBookServiceImpl implements AddBookService {
    @Autowired
    private AddBookMapper addBookMapper;

    /**
     * 获取用户的地址簿
     * @return
     */
    @Override
    public List<AddressBook> getAll() {
        Long userId = BaseContext.getCurrentId();
        List<AddressBook> bookList = addBookMapper.getAll(userId);
        return bookList;
    }

    /**
     * 新增地址簿
     * @param addressBook
     */
    @Override
    public void save(AddressBook addressBook) {
        Long userId = BaseContext.getCurrentId();
        addressBook.setUserId(userId);
        addressBook.setIsDefault(0);
        addBookMapper.insert(addressBook);
    }

    /**
     * 根据Id查询地址簿
     * @param id
     * @return
     */
    @Override
    public AddressBook getById(Long id) {
        AddressBook addressBook = addBookMapper.getById(id);
        return addressBook;
    }

    /**
     * 修改地址簿
     * @param addressBook
     */
    @Override
    public void update(AddressBook addressBook) {
        addBookMapper.update(addressBook);
    }

    /**
     * 根据Id删除地址簿
     * @param id
     */
    @Override
    public void deleteById(Long id) {
        addBookMapper.deleteById(id);
    }

    /**
     * 查询默认地址簿
     * @param addressBook
     * @return
     */
    @Override
    public List<AddressBook> getDefault(AddressBook addressBook) {
        return addBookMapper.getDefault(addressBook);
    }

    /**
     * 设置默认地址簿
     * @param addressBook
     */
    @Override
    public void setDefault(AddressBook addressBook) {
        //首先将所有的都设置为非默认地址
        addBookMapper.setAllDefault();
        //再将指定默认地址设置为默认值
        addressBook.setIsDefault(1);
        addBookMapper.update(addressBook);
    }
}
